package com.raising.sc.controller;

import com.raising.sc.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }

    /**
     * 通过服务id获取服务的地址
     * 这实际上是ribbon通过服务名能够找到对应服务的ip的核心
     * @return
     */
    @GetMapping("ribbon")
    public String ribbon(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-C");
        return serviceInstance.getUri().toString();
    }

}
